package sbc.ejb3;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import org.hibernate.validator.NotNull;

import sbc.ejb3.dao.CallbackHandler;

@Entity
@EntityListeners({CallbackHandler.class})
@Table(name="Sensor")
@NamedQueries({
    @NamedQuery(
        name="GemesseneUnmontierteSensoren",
        query=
            "SELECT s " +
            "FROM Sensor s " +
            "WHERE (s.feuchtigkeit IS NOT NULL OR " +
            "s.temperatur IS NOT NULL OR " +
            "s.helligkeit IS NOT NULL) AND " +
            "NOT EXISTS (" +
	        	"SELECT platine " +
	        	"FROM Platine platine " +
	        	"WHERE platine.firstSensor = s)" + 
            "AND " + 
            "NOT EXISTS (" +
            	"SELECT platine " +
            	"FROM Platine platine " +
            	"WHERE platine.secondSensor = s)"
    ),
    @NamedQuery(
        name="UngemesseneSensoren",
        query=
            "SELECT s " +
            "FROM Sensor s " +
            "WHERE s.feuchtigkeit IS NULL AND " +
            "s.temperatur IS NULL AND " +
            "s.helligkeit IS NULL"
    )
})
public class Sensor implements java.io.Serializable {
	
	private static final long serialVersionUID = 1L;
	
	@Id @GeneratedValue
    @Column(name="messungId")
	private Integer id;

    @Column(name="sensorId")
    @NotNull
	private String sensorId;  
	
	@OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="helligkeitId", nullable=true, updatable=true, unique=false)
	private Messung helligkeit;
	
	@OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="feuchtigkeitId", nullable=true, updatable=true, unique=false)
	private Messung feuchtigkeit;
	
	@OneToOne(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
    @JoinColumn(name="temperaturId", nullable=true, updatable=true, unique=false)
	private Messung temperatur;
		
	public Sensor() { }

	public Sensor(String sensorId) {
		super();
		this.sensorId = sensorId;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getSensorId() {
		return sensorId;
	}

	public void setSensorId(String sensorId) {
		this.sensorId = sensorId;
	}

	public Messung getFeuchtigkeit() {
		return feuchtigkeit;
	}

	public void setFeuchtigkeit(Messung feuchtigkeit) {
		this.feuchtigkeit = feuchtigkeit;
	}

	public Messung getHelligkeit() {
		return helligkeit;
	}

	public void setHelligkeit(Messung helligkeit) {
		this.helligkeit = helligkeit;
	}

	public Messung getTemperatur() {
		return temperatur;
	}

	public void setTemperatur(Messung temperatur) {
		this.temperatur = temperatur;
	}
	
	public String toString() {
		String string = sensorId;
		if (id != null)
			string += " (" + id + ")";
		if (feuchtigkeit != null)
			string += " f: " + feuchtigkeit.toString();
		if (helligkeit != null)
			string += " h: " + helligkeit.toString();
		if (temperatur != null)
			string += " t: " + temperatur.toString();
		return string;		
	}	
}